//
//  ViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/1.
//  Copyright © 2016年 郑东喜. All rights reserved.
//  QQ、微信第三方测试界面

import UIKit

class ViewController: UIViewController,UIAlertViewDelegate,testDelegate {

    let tool = TencentTool()
    let appDelegate = QQTool()
//    let wxTool = Weixin()
    let wxtool = WXTool()
    
    var wxType = NSInteger()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addBtn()
        
        let tfNum = "18905036476"
        
        let tfPass : String = (Encryption.md5Encrypt(with: "123"))
        
        print(tfPass)
        
        let url = URL(string:"http://bzg.ie1e.com/APP/User_UserLogin/Login?tel=18905036476&pwd=00E9CD60620FFE2405230B3A60CCCA54")
        
        let request = NSMutableURLRequest(url: url!)
        
        request.httpMethod = "POST"
        
        let dataTask = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil {
                print(error as Any)
            } else {
                let json = JSON(data: data!)
                print(json["Data"]["user"][0]["uid"])

                for dic in json["Data"]["user"] {
                    print(dic)
                }
            }
        }
        dataTask.resume()

    }
    
    
}

// MARK:- 微信
extension ViewController {
    

    //微信支付
    @IBAction func wxPay(_ sender: UIButton) {
        
        Weixin.weixinPay()
        
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("微信pay")))")
        
        
    }
    
    //微信分享
    @IBAction func wxShare(_ sender: Any) {
        Weixin.weixinShare(withDescription: "分享")
        
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("微信分享")))")
    }
    
    //微信登陆
    @IBAction func wxLogin(_ sender: UIButton) {

//        wxTool.clickAuthButton()
//        Weixin.shareDataSource().clickAuthButton()
        wxtool.clickAuto()
        
    }
}

// MARK:- QQ
extension ViewController {
    
    @IBAction func qqLogin(_ sender: Any) {
//        tool.qqLogin()
        QQTool.qqTool.qqLogin()
    }
    
    @IBAction func qqShare(_ sender: Any) {
//        tool.qqShare()
        QQTool.qqTool.qqShare()
        appDelegate.delegate = self
    }

}

extension ViewController {
    
    func shareSuccssWithQQCode(str: NSInteger) {
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        
        if str == 0 {
            let alertController = UIAlertController(title: "温馨提示", message: "分享成功", preferredStyle: UIAlertControllerStyle.alert)
            
            alertController.addAction(UIAlertAction(title: "确认", style: UIAlertActionStyle.default, handler: nil))
            
            self.present(alertController, animated: true, completion: nil)
            
        } else {
            let alertController = UIAlertController(title: "温馨提示", message: "分享失败", preferredStyle: UIAlertControllerStyle.alert)
            
            alertController.addAction(UIAlertAction(title: "确认", style: UIAlertActionStyle.default, handler: nil))
            
            self.present(alertController, animated: true, completion: nil)
        }
    }
}


extension ViewController {
    
    func addBtn() -> Void {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        btn.backgroundColor = UIColor.red
        btn.addTarget(self, action: #selector(tapped), for: .touchUpInside)
        view.addSubview(btn)
    }
    
    func tapped() -> Void {
        print("ddd")
    }
}
